package megrez.job.custom.web;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import lombok.RequiredArgsConstructor;
import megrez.job.custom.component.TaskService;
import megrez.job.custom.domain.Config;
import megrez.job.custom.domain.ConfigRepository;
import megrez.job.custom.domain.ConfigService;
import megrez.job.custom.domain.ConfigSpecifications;
import megrez.job.custom.web.vo.ConfigInput;
import megrez.job.custom.web.vo.ConfigVO;

/**
 * 任务属性接口
 * 
 * @author Lucky Yang
 * @since 2.6.9
 */
@RestController
@RequestMapping("/v1/configs")
@RequiredArgsConstructor
public class ConfigResource {
    private final ConfigRepository configRep;
    private final ConfigService configSrc;
    private final TaskService taskSrc;

    @PostMapping()
    @ResponseStatus(HttpStatus.CREATED)
    public ConfigVO create(@RequestBody ConfigInput vo) {

        Config config = vo.copyTo(new Config());

        return ConfigVO.of(taskSrc.createConfig(config));
    }

    @PutMapping("/{id}")
    public ConfigVO update(@PathVariable("id") String id, @RequestBody ConfigInput vo) {
        Config config = configSrc.findById(id);
        config = vo.copyTo(config);

        return ConfigVO.of(taskSrc.updateConfig(config));
    }

    @DeleteMapping("/{id}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void delete(@PathVariable("id") String id) {
        taskSrc.removeConfig(id);
    }

    @GetMapping("/{id}")
    public ConfigVO getById(@PathVariable("id") String id) {
        return ConfigVO.of(configSrc.findById(id));
    }

    @GetMapping()
    public Page<ConfigVO> getConfigPage(@RequestParam(value = "className", required = false) String className,
            @RequestParam(value = "enabled", required = false) Boolean enabled,
            Pageable pageable) {

        Specification<Config> where = ConfigSpecifications.empty();
        if (StringUtils.hasText(className)) {
            where = where.and(ConfigSpecifications.classNameLike(className));
        }
        if (enabled != null) {
            where = where.and(ConfigSpecifications.enabledEqual(enabled));
        }

        Page<Config> page = configRep.findAll(where, pageable);
        return page.map(ConfigVO::of);
    }

}
